/**
 * @author Thomas Pfeiffer
 * @file   Nortel.h
 * @brief  Implementation of the class Nortel
 */
#include <string>
#include "Nortel.h"
#include "CrypterFactory.h"

void Nortel::Encipher( TEncoding enc, std::string const & fileName )
{
	Crypter *crypt = getCrypt(enc);  // Try to allocate a concrete crypto algorithm

	if (crypt == 0) {  // Check if we got a valid object / pointer
		throw "Unknown encoding...";
	}

	if(!crypt->ReadFile(fileName)) throw "ReadFile: Error in reading...";
	crypt->Encrypt();
	if(!crypt->WriteFile(crypt->getOutputFilename(fileName))) throw "WriteFile: Error in writing...";

	delete crypt;
}

void Nortel::Decipher( TEncoding enc, std::string const & fileName )
{
	Crypter *crypt = getCrypt(enc); // Try to allocate a concrete crypto algorithm

	if (crypt == 0) { // check if we got a valid pointer
		throw "Unknown encoding...";
	}

	if(!crypt->ReadFile(fileName)) throw "ReadFile: Error in reading...";
	crypt->Decrypt();
	if(!crypt->WriteFile(crypt->getRawFilename(fileName))) throw "WriteFile: Error in writing...";

	delete crypt;
}

// Maps between the two algorithm enums.
Crypter * Nortel::getCrypt(TEncoding enc) {
	switch (enc) {
	case eCaesar:
		return CrypterFactory::CreateCrypter(CrypterFactory::eCaesar);
		break; // not necessary
	case eRSA:
		return CrypterFactory::CreateCrypter(CrypterFactory::eRSA);
		break; // not necessary
	}

	return 0;
}